package NsingCHM.Files;

import MOLE.*;

/** 
 XmlHTTP ��ժҪ˵����
*/
public class XmlHTTP
{
	public XmlHTTP()
	{
		//
		// TODO: �ڴ˴���ӹ��캯���߼�
		//
	}

	public static String getData(String url, String pagecode)
	{
		String PageUrl = url;
		WebClient wc = new WebClient();
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: Byte[] pageData = wc.DownloadData(PageUrl);
		byte[] pageData = wc.DownloadData(PageUrl);
		String s = "";

		if (pagecode.toLowerCase().equals("gb2312"))
		{
			s = Encoding.Default.GetString(pageData);
		}
		else //utf8
		{
			s = Encoding.UTF8.GetString(pageData);
			;

		}
		return s;
	}


	public static String Get_Http(String a_strUrl, int timeout)
	{
		try
		{
			HttpWebRequest request = (HttpWebRequest) WebRequest.Create(a_strUrl);
			request.Timeout = timeout;
			HttpWebResponse response = (HttpWebResponse) request.GetResponse();
			StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
			StringBuilder builder = new StringBuilder();
			while (-1 != reader.Peek())
			{
				builder.append(reader.ReadLine());
			}
			return builder.toString();
		}
		catch (RuntimeException exception)
		{
			return ("����" + exception.getMessage());
		}
	}

	public static String PostXmlToUrl(String url, String data)
	{
		HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
		hwr.Method = "POST";

		Stream stream = hwr.GetRequestStream();

		StreamWriter sw = new StreamWriter(stream,System.Text.Encoding.UTF8);
		sw.Write(data);
		sw.Close();

		stream = hwr.GetResponse().GetResponseStream();

		StreamReader sr = new StreamReader(stream,System.Text.Encoding.UTF8);
		String ret = sr.ReadToEnd();
		sr.Close();

		return ret;
		//			 
		//			 ��������Ӧ�Ļ�ֻ��Ҫ2�д��룬
		//byte[]   buf   =   Request.BinaryRead(Request.ContentLength);   
		//  string   str   =   System.Text.Encoding.UTF8.GetString(buf);   
		//�������ܵõ���
		//��ʵ���ϴ���Ϳ��Խ���һ���򵥵ķ�������Ľ���ģʽ��ģ���b/s�ܹ�����ʹ��webservice��������佻����࣬soap��http��ʵ�Ƚ����Ƶġ� 


	}

	private String HttpPost(String URI, String Parameters)
	{
		//			System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
		//			req.Proxy = new System.Net.WebProxy(ProxyString, true); 
		//
		//			req.ContentType = "application/x-www-form-urlencoded";
		//			req.Method = "POST"; 
		//
		//			byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
		//			req.ContentLength = bytes.Length; 
		//
		//			System.IO.Stream os = req.GetRequestStream ();
		//			os.Write (bytes, 0, bytes.Length); 
		//			os.Close (); 
		//
		//			System.Net.WebResponse resp = req.GetResponse();
		//			if (resp== null) return null; 
		//
		//			System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
		//	return sr.ReadToEnd().Trim();
		return "";
	}

	/** 
	 
	 
	 @param url
	 @param Parameters
	*/
	public static String Post(String url, String Parameters)
	{

		//	�ӱ���ɿ�����������������һ����userid��һ����password��������POST��ʽ�ύ������Ӧ�ð����������
		//����POST�����ݸ�ʽΪ��
		//���������1=ֵ1&���������2=ֵ2&���������3=ֵ3����
		//Ҫע����ǡ�ֵ�������Ǿ���HTMLEncode�ģ������ܰ�����<>=&����Щ���š�
		//
		//������Ҫ�ύ������Ӧ���ǣ�
		//userid=value1&password=value2


		String strId = "guest";
		String strPassword = "123456����";
		//string url="http://localhost:777/5186/post2.aspx";
		//strPassword=Server.HtmlEncode(strPassword);

		ASCIIEncoding encoding = new ASCIIEncoding();
		String postData = "userid=" + strId;
		postData += "&password=" + strPassword;
		postData = Parameters;

//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] data = encoding.GetBytes(postData);
		byte[] data = encoding.GetBytes(postData);

		// Prepare web request...
		HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);

		myRequest.Method = "POST";
		myRequest.ContentType = "application/x-www-form-urlencoded";
		myRequest.ContentLength = data.length;
		Stream newStream = myRequest.GetRequestStream();

		// Send the data.
		newStream.Write(data,0,data.length);
		newStream.Close();


		//new add by ngailon
		try
		{
			//myRequest.ContentType="text/html;charset=GBK";
			Stream stream = myRequest.GetResponse().GetResponseStream();
			StreamReader sr = new StreamReader(stream,System.Text.Encoding.GetEncoding("GB2312"));
			//StreamReader   sr   =   new   StreamReader(stream,System.Text.Encoding.GetEncoding());   
			String ret = sr.ReadToEnd();
			sr.Close();
			return ret;
		}
		catch (RuntimeException ee)
		{
			return ee.toString();
		}


	}



	public static void PostReceive()
	{
		//userid=guest&password=123456���� 
		//			byte[]   buf   =   Request.BinaryRead(Request.ContentLength);   
		//			string   str   =   System.Text.Encoding.UTF8.GetString(buf); 
	}





}